package com.explorer.ider.bootguide.fragment;

import android.animation.ValueAnimator;
import android.app.AlarmManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.explorer.ider.bootguide.MainActivity;
import com.explorer.ider.bootguide.R;
import com.explorer.ider.bootguide.adapter.MyTimeZoneListAdapter;
import com.explorer.ider.bootguide.utils.TimeZoneUtils;


/**
 * Created by Eric on 2016/9/5.
 */
public class FragmentDateTime extends Fragment{

    private static final String TAG = "FragmentDateTime";
    private View views;
    private AlarmManager apiAlarmManager;
    private TimeZoneUtils mTimeZoneUtils;
    private String[][] mTimeZoneTypeList;
    private ListView mTimezoneListView;
    private CardView mTimeZoneLockCardView;
    private LinearLayout hourTimeImage,minuteTimeImage;
    private Button mPagerBackButton;
    private MyTimeZoneListAdapter myTimeZoneListAdapter;
    private Time t=new Time("GMT+0");
    private int hour = t.hour;
    private int minute = t.MINUTE;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (views == null){
            views = inflater.inflate(R.layout.fragment_date_time, container,false);
            initData();
            initView();
        }
        return views;
    }

    public void initData(){
        apiAlarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
        mTimeZoneUtils = new TimeZoneUtils(apiAlarmManager);
        mTimeZoneTypeList = mTimeZoneUtils.getTimeZoneList();
        myTimeZoneListAdapter = new MyTimeZoneListAdapter(getActivity(),mTimeZoneTypeList,mTimeZoneUtils);
        t=new Time("GMT+00:00");
        hour = t.hour;
        minute = t.MINUTE;


    }

    private void initView(){
        final Animation mAnimationIn , mAnimationOut;
        mAnimationIn = AnimationUtils.loadAnimation(getActivity(), R.anim.view_focus_animation_in);
        mAnimationOut = AnimationUtils.loadAnimation(getActivity(),R.anim.view_focus_animation_out);
        mAnimationIn.setFillAfter(true);
        mAnimationOut.setFillAfter(true);
        hourTimeImage = (LinearLayout) views.findViewById(R.id.id_time_image_hour_view);
        minuteTimeImage = (LinearLayout) views.findViewById(R.id.id_time_image_minute_view);
        mTimeZoneLockCardView = (CardView) views.findViewById(R.id.id_time_zone_focus_lock_card_view);
        mTimezoneListView = (ListView) views.findViewById(R.id.id_time_zoom_list);
        mPagerBackButton = (Button) views.findViewById(R.id.id_time_zone_back_button);
        mTimezoneListView.setAdapter(myTimeZoneListAdapter);
        hourTimeImage.setRotation(hour);
        minuteTimeImage.setRotation(minute);

        mPagerBackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(MainActivity.ACTION_TO_BACK_PAGER2);
                getActivity().sendBroadcast(intent);
            }
        });
        mPagerBackButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    v.startAnimation(mAnimationIn);
                    mTimeZoneLockCardView.setVisibility(View.INVISIBLE);
                }else {
                    v.startAnimation(mAnimationOut);
                    mTimeZoneLockCardView.setVisibility(View.VISIBLE);
                }
            }
        });
        mTimezoneListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mTimeZoneUtils.setTimeZone(mTimeZoneTypeList[i][1]);
                startAnimation(view,i);
                Intent intent = new Intent();
                intent.setAction(MainActivity.ACTION_TO_NEXT_PAGER3);
                getActivity().sendBroadcast(intent);
            }
        });
        mTimezoneListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                setClockImageView(mTimeZoneTypeList[i][0]);
                startAnimation(view,i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }


    public void startAnimation(View v,int position){
        final ValueAnimator animator = ValueAnimator.ofInt((int)mTimeZoneLockCardView.getY(),(int)v.getY()+3);
        animator.setDuration(200);
        animator.setInterpolator(new LinearInterpolator());//线性效果变化
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Integer integer = (Integer) animator.getAnimatedValue();
                mTimeZoneLockCardView.setY(integer);
            }
        });
        animator.start();
    }

    private void setClockImageView(String timeGapInfo){
        Log.i(TAG, "1setClockImageView: " + timeGapInfo);

        Log.i(TAG, "setClockImageView: " + hour + "---" +minute);
        String timeInfo = timeGapInfo.substring(3,6);
        int timeGap = 0;
        int oneHourAngle = 360/12;
        int oneMinuteAngle = 360/60;
        if (timeInfo.substring(0,1).equals("-")){
            timeInfo = timeInfo.replace("-" ,"0");
            timeGap = hour - (Integer.parseInt(timeInfo));
        }else{
            timeInfo = timeInfo.replace("+" ,"0");
            timeGap = hour + (Integer.parseInt(timeInfo));
        };
        final ValueAnimator animator = ValueAnimator.ofInt((int)hourTimeImage.getRotation(),timeGap * oneHourAngle);
        animator.setDuration(200);
        animator.setInterpolator(new BounceInterpolator());//弹性效果变化
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
            Integer integer = (Integer) animator.getAnimatedValue();
            hourTimeImage.setRotation(integer);
            }
        });
        animator.start();
        minuteTimeImage.setRotation((float) minute * oneMinuteAngle);
    }

}
